<?php
##############################################
#
#   奖品表
# @ Time : 2017-1-3 - 下午10:45:24
# @ Author : JiangYongWei <1085562810@qq.com>
# @ Encoding : UTF-8 language : PHP
#
##############################################

namespace Admin\Controller;
use Admin\Model\PrizeModel;


class PrizeController extends AdminBaseController {
	
	private $_prize;
	
	public function _initialize(){
		parent::_initialize();
		$this -> _prize  = new PrizeModel();
	}
	
	/**
	 * 商品列表
	 */
	public function prizeList(){
 		$data = $this -> _prize -> getPrize(I('prize_name'), I('prize_type'));
		$this -> assign('rule', $data[0]);
		$this -> assign('page', $data[1]);
		$this -> display('index');
	}

	public function addPrize(){
		if(IS_GET){
			$this -> display('add');
		}else if(IS_POST){
			$data = I('post.');
            $data['use_start_date'] = strtotime(I('use_start_date'));
            $data['use_end_date'] = strtotime(I('use_end_date'));
			$data['add_time'] = time();
			$total_prize_num = $this -> _prize -> getAllPrizeNum();
			$data['rate'] = $data['prize_num'] / array_sum($total_prize_num);
			$result = $this -> _prize -> addPrize($data);
			if($result)
				$this -> success('添加成功', U('prizeList'), 2);
			else
				$this -> error('操作失败', '', 2);
		}
	}

	/**
	  *修改奖品	
	  */
	public function editPrize(){
		if(IS_GET){
			$p_id = I('p_id');
			$data = $this -> _prize -> getPrizeFind($p_id);
			$this -> assign('p_id', $p_id);
			$this -> assign('prize', $data);
			$this -> display('edit');
		}else if(IS_POST){
			$data = I('post.');
            $data['use_start_date'] = strtotime(I('use_start_date'));
            $data['use_end_date'] = strtotime(I('use_end_date'));
			$data['add_time'] = time();
			$total_prize_num = $this -> _prize -> getAllPrizeNum();
			$data['rate'] = $data['prize_num'] / array_sum($total_prize_num);
			$result = $this -> _prize -> editPrize($data);
			if($result !== false)
				$this -> success('修改成功', U('prizeList'), 2);
			else
				$this -> error('操作失败', '', 2);

		}
		
		
	}

	/**
	 * 删除商品
	 */
	public function deletePrize(){
		if(!IS_AJAX) return;
		$result = $this -> _prize -> deletePrize(intval($_GET['p_id']));
		if($result)
			$this -> ajaxReturn(array('status' => 1,'message' => '删除成功'));
		else
			$this -> ajaxReturn(array('status' => 2,'message' => '操作失败'));
	}
	
	public function deleteAllPrize(){
	    if(!IS_POST) return;
	    $result = $this -> _prize -> deleteAllPrize($_POST);
	    if($result)
				$this -> success('删除成功', U('prizeList'), 2);
		else
				$this -> error('操作失败', '', 2);
	}



	/**
     *修改时商品类型
     * @Date 201705
     * @Author Linsenquan
     */
    public function prizeTypeEdit()
    {
        $id = I('id');
        $data = httpUrl(C('queryCategoryList'), '', 0, $header = array(), $verify = false);
        $data_option_list = json_decode($data,true);
        $temArr = explode('</option>',$data_option_list['option_list']);
        $dataArr = array();
        foreach ($temArr as $k => $v){
            if($v == ''){
                continue;
            }
            $dataArr[$k]['type'] = $v.'</option>';
        }
        $dis_data = array();
        $html_string = '';
        foreach($dataArr as $k=>$v){
            $first = strpos($v['type'], '"')+1;
            $last = strrpos($v['type'], '"');
            if($id == substr($v['type'], $first, ($last-$first))){
                $dis_data[$k]['type'] = str_replace('<option', '<option selected="true"',$v['type']);
            }else{
                $dis_data[$k]['type'] = $v['type'];
            }
            $html_string .= $dis_data[$k]['type'];
        }
        echo $html_string;
    }

    /**
     *商品类型
     * @Date 201705
     * @Author Linsenquan
     */
    public function prizeTypeList()
    {
        $data = httpUrl(C('queryCategoryList'), '', 0, $header = array(), $verify = false);
        echo $data;
    }
	
}



	
	